#!/usr/bin/perl

package Login;
use strict;
use Data::Dumper;

sub new($)
{
    my $class = shift;
    
    my $self = {};
    $self->{EM_IDS} = {};
    $self->{RECORDS} = {};
    
    bless ($self, $class);
    
    return $self;
}

sub Insert($$$)
{
    my $self = shift;
    my $emid = shift;
    my $datetime = shift;
    
    $self->{EM_IDS}->{$emid} = 1;
    
    my $v_date = substr($datetime, 0, 8);
    $v_date = substr($v_date, 0, 4) . "-" . substr($v_date, 4, 2) . "-" . substr($v_date, 6);
    my $v_time = substr($datetime, 8);
    $v_time = substr($v_time, 0, 2) . ":" . substr($v_time, 2, 2) . ":" . substr($v_time, 4);
    
    push @{$self->{RECORDS}->{$emid}->{$v_date}}, "$v_time";
}

sub GetAllIds($)
{
    my $self = shift;
    
    return sort keys %{$self->{EM_IDS}};
}

sub GetAllDates($)
{
    my $self = shift;
    my %dates = ();
    
    for my $id (keys %{$self->{EM_IDS}}) {
        for my $date (keys %{$self->{RECORDS}->{$id}}) {
            $dates{$date} = 1;
        }
    }
    
    return sort keys %dates;
}

sub GetTime($$$)
{
    my $self = shift;
    my $emid = shift;
    my $date = shift;

    return $self->{RECORDS}->{$emid}->{$date} ? 
        "'" . join(" ", @{$self->{RECORDS}->{$emid}->{$date}}) . "'" : "";    
}
    
package main;

use strict;
use Win32::GUI;
use Data::Dumper;

use constant {
    PADX => 10,
    PADY => 5,
    TEXTLEN => 400,
    FONTSIZE => 15 
};

my $font = Win32::GUI::Font->new(-name => "Georgia", -size => FONTSIZE);

my $win = Win32::GUI::Window->new(
        -title => "Data Convert",
        -name => "Main",
        -font => $font,
        -size => [800,600],
        -minsize => [300,200],
    );

my $lblInput = $win->AddLabel(-text => "Input file",
        -font => $font, 
        -valign => "center",
        -pos => [PADX, PADY*2],
        -height => FONTSIZE*2);

my $txtInput = $win->AddTextfield(
        -font => $font,
        -left => $lblInput->Left() + $lblInput->Width() + PADX, 
        -top => $lblInput->Top(),
        -size => [TEXTLEN, FONTSIZE*2]);

my $btnInput = $win->AddButton(-name => "btnInput", 
        -text => "Browse", 
        -font => $font,
        -left => $txtInput->Left() + $txtInput->Width() + PADX, 
        -top => $txtInput->Top(),
        -height => FONTSIZE*2);

my $lblSave = $win->AddLabel(-text => "Save to",
        -font => $font,
        -left => $lblInput->Left(), 
        -top => $lblInput->Top() + $lblInput->Height() + PADY,
        -width => $lblInput->Width(),
        -height => FONTSIZE*2);

my $txtSave = $win->AddTextfield(
        -font => $font,
        -size => [TEXTLEN, FONTSIZE*2],
        -left => $txtInput->Left(), 
        -top => $lblSave->Top());

my $btnSave = $win->AddButton(-name => "btnSave",
        -text => "Browse", 
        -font => $font,
        -left => $txtSave->Left() + $txtSave->Width() + PADX, 
        -top => $txtSave->Top(),
        -height => FONTSIZE*2);

my $btnConvert = $win->AddButton(-name => "btnConvert",
        -text => "Convert",
        -font => $font,
        -left => $lblSave->Left(), 
        -top => $lblSave->Top() + $lblSave->Height() + PADY,
        -width => $lblInput->Width() + $txtInput->Width() + PADX,
        -height => FONTSIZE*2);

my $btnExit = $win->AddButton(-name => "btnExit",
        -text => "Exit",
        -font => $font,
        -left => $btnSave->Left(), 
        -top => $btnConvert->Top(),
        -width => $btnSave->Width(),
        -height => FONTSIZE*2);


$win->Change(-width => $btnInput->Left() + $btnInput->Width() + PADX*3,
             -height => $btnConvert->Top() + $btnConvert->Height());
$win->Move(200,200);
$win->Show();
Win32::GUI::Dialog();

sub Main_Terminate 
{
    return -1;
}

sub btnInput_Click
{
    my $file = Win32::GUI::GetOpenFileName(
        -owner => $win,
        -title => "Select a File to Convert", 
        -filter => ["TXT - Text file", "*.txt", 
                    "All files", "*.*"],
    );

    $txtInput->Text($file) if ($file);
}

sub btnSave_Click 
{
    my $file = Win32::GUI::GetSaveFileName(
        -owner => $win,
        -title => "Save File to", 
        -filter => ["CSV - Common Seperate Value file", "*.csv", 
                    "All files", "*.*"],
    );

    $txtSave->Text($file) if ($file);
}

sub btnConvert_Click
{
    my $file_in = $txtInput->Text();

    open(IFH, $file_in) || Die("File not found: $file_in");
    
    my $login = Login->new();
    
    while (<IFH>) {
        next if (/^#/);
        next if (/^\s*$/);

        s/^\s+//;
        s/\s+$//;
        
        my @fields = split /\t/;
                
        $login->Insert($fields[1], $fields[2]);
                    
    } 
    close(IFH);
    
    my $file_out = $txtSave->Text();
    open(OFH, ">$file_out") || Die("Cannot writ to $file_out");
 
    my @allids = $login->GetAllIds();
    my @alldates = $login->GetAllDates();    
    
#    print "@allids\n";
#    print "@alldates\n";
    print OFH "'ID','" . join("','", @alldates) . "'\n";
    for my $emid (@allids) {
        print OFH "'$emid'";
        for my $date (@alldates) {
            print OFH ","  . $login->GetTime($emid, $date);
        }
        print OFH "\n";
    }
    close(OFH);
    
    Win32::GUI::MessageBox($win, "Convert OK", "Convert", MB_OK);

}

sub btnExit_Click
{
    exit (0);
}

sub Die($)
{
    my $msg = shift;

    Win32::GUI::MessageBox($win, $msg, "Error", MB_OK);

    exit(1);
}
